package bookManage.Controller.servlet;

import bookManage.Model.entity.BookShow;
import bookManage.Model.service.ServiceImpl;
import bookManage.utils.JsonUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @BelongsProject: library-management-system-master
 * @BelongsPackage: bookManage.Controller.servlet
 * @Author: yuan wang
 * @CreateTime: 2023-02-07  16:28
 * @Description: 实现模糊查询
 * @Version: 1.0
 */
@WebServlet(name = "DimQueryBook", value = "/manager/DimQueryBook")
public class DimQueryBook extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String jsonString = String.valueOf(JsonUtil.getJson(req));
        JSONObject jsonObject = JSON.parseObject(jsonString);

        ServiceImpl service = new ServiceImpl();

        String bookName = jsonObject.getString("bookName");

        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();

        try {
            List<BookShow> bookShowList = service.dimQueryBook(bookName);
            if (bookShowList == null || bookShowList.size() < 1) {
                object.put("code", "-1");
                object.put("message", "无此书，请修改查询内容重新查询，更加模糊的字段更容易查到书籍");
                out.println(object);
            } else {
                object.put("code", "200");
                jsonString = JSONObject.toJSONString(bookShowList);
                object.put("bookList", jsonString);
                out.println(object);
            }
        } catch (SQLException e) {
            object.put("code", "-1");
            object.put("message", "查询失败");
            out.println(object);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }
}
